/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import {
  getPrintKeyList,
  addPrintKey,
  deletePrintKey,
  checkPrintKey,
  refreshStatus,
} from "@/services/allocation";
import { isLegalData } from "@/utils/utils";
import { message } from "antd";

export default {
  namespace: "printKey",

  state: {
    list: [],
  },

  effects: {
    // 密钥配置，获取列表
    *getPrintKeyList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getPrintKeyList, payload);
      if (!response) return;
      const { code, data, msg } = response;
      if (code == 0 && data) {
        yield put({
          type: "save",
          payload: {
            list: isLegalData(data, []),
          },
        });
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 密钥配置，添加密钥
    *addPrintKey({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(addPrintKey, payload);
      if (!response) return;
      const { code } = response;

      __dva_resolve(response);
      if (code == 0) {
        message.success("添加成功！");
      }
    },
    // 密钥配置，删除密钥
    *deletePrintKey({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(deletePrintKey, payload);
      if (!response) return;
      const { code, msg } = response;

      if (code == 0) {
        __dva_resolve(response);
        message.success("删除成功！");
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 密钥配置，刷新状态
    *refreshStatus({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(refreshStatus, payload);
      if (!response) return;
      const { code, msg } = response;
      if (code == 0) {
        __dva_resolve(response);
        message.success("刷新成功！");
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
  },
  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
